/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core;

import javax.servlet.jsp.tagext.SimpleTagSupport;


/**
 * <p>Base class for tags that render to links capable of including JavaScript
 * event handlers and/or CSS Style attributes. This class does not implement
 * the doTag() method. Subclasses should provide appropriate implementation of this.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>disabled</b> - whether this element is disabled or not. For example, this
 * can be used for buttons to make them not to respond to clicks. Can be
 * <code>&quot;true&quot;</code> or <code>&quot;false&quot;</code>, default is
 * <code>&quot;false&quot;</code>
 * </li>
 * <li>
 * <b>style</b> - CSS styles that will be applied to this resulting element. For
 * example, <code>&quot;visibility: visible; z-index: 10;&quot;</code>
 * </li>
 * <li>
 * <b>styleClass</b> - class that will be assigned to resulting element with
 * HTML <code>'class'</code> attribute
 * </li>
 * <li>
 * <b>tagId</b> - id that will be assigned to resulting element with HTML
 * <code>'id'</code> attribute
 * </li>
 * <li>
 * <b>alt</b> - alternate text for generated element
 * </li>
 * <li>
 * <b>title</b> - advisory title for generated element
 * </li>
 * <li>
 * <b>onchange</b> - javascript handler -- code, that is called when element
 * contents changes
 * </li>
 * <li>
 * <b>onfocus</b> - javascript handler -- code, that is called when element
 * gains input focus
 * </li>
 * <li>
 * <b>onblur</b> - javascript handler -- code, that is called when element
 * loses input focus
 * </li>
 * <li>
 * <b>onclick</b> - javascript handler -- code, that is called when element
 * is clicked
 * </li>
 * <li>
 * <b>ondblclick</b> - javascript handler -- code, that is called when
 * element is double-clicked
 * </li>
 * <li>
 * <b>onkeydown</b> - javascript handler -- code, that is called when
 * keyboard key is pressed
 * </li>
 * <li>
 * <b>onkeyup</b> - javascript handler -- code, that is called when
 * keyboard key is released
 * </li>
 * <li>
 * <b>onkeypressed</b> - javascript handler -- code, that is called when
 * keyboard key is pressed and released
 * </li>
 * <li>
 * <b>onmousedown</b> - javascript handler -- code, that is called when
 * mouse button is pressed
 * </li>
 * <li>
 * <b>onmouseup</b> - javascript handler -- code, that is called when
 * mouse button is released
 * </li>
 * <li>
 * <b>onmousemove</b> - javascript handler -- code, that is called when
 * mouse is moved over element
 * </li>
 * <li>
 * <b>onmouseover</b> - javascript handler -- code, that is called when
 * mouse enters element
 * </li>
 * <li>
 * <b>onmouseout</b> - javascript handler -- code, that is called when
 * mouse leaves element
 * </li>
 * <li>
 * <b>accesskey</b> - key (accelerator) that will be used to immediately access
 * element
 * </li>
 * <li>
 * <b>tabindex</b> - index that influences the order in which controls are
 * selected with tab key
 * </li>
 * </ul>
 * </p>
 * <p><a href="BaseHandlerTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.12 $ $Date: 2006/08/03 10:08:08 $
 * @see org.apache.struts.taglib.html.BaseHandlerTag
 */
public class BaseHandlerTag extends SimpleTagSupport {

	// ~ Instance variables

	// ~ Common attributes

	/**
	 * Whether or not this element is disabled (cannot be pressed, for example)
	 */
	protected Boolean disabled = Boolean.FALSE;

	/**
	 * CSS styles to be applied to this HTML element.
	 */
	protected String style = new String();

	/**
	 * CSS stylesheet class to be applied to this HTML element (renders a "class" attribute).
	 */
	protected String styleClass = new String();

	/**
	 * The ID of the generated tag
	 */
	protected String tagId = new String();

	/**
	 * The alternate text of this element.
	 */
	private String alt = null;

	/**
	 * The advisory title of this element.
	 */
	private String title = null;

	// ~ JavaScript events properties

	/**
	 * JavaScript event handler that is executed when this element's content changes.
	 */
	protected String onchange;

	/**
	 * JavaScript event handler that is executed when this element loses input focus.
	 */
	protected String onblur;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse click.
	 */
	protected String onclick;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse double click.
	 */
	protected String ondblclick;

	/**
	 * JavaScript event handler that is executed when this element receives input focus.
	 */
	protected String onfocus;

	/**
	 * JavaScript event handler that is executed when this element receives a key down event.
	 */
	protected String onkeydown;

	/**
	 * JavaScript event handler that is executed when this element receives a key press event.
	 */
	protected String onkeypress;

	/**
	 * JavaScript event handler that is executed when this element receives a key up event.
	 */
	protected String onkeyup;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse down event.
	 */
	protected String onmousedown;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse move event.
	 */
	protected String onmousemove;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse out event.
	 */
	protected String onmouseout;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse over event.
	 */
	protected String onmouseover;

	/**
	 * JavaScript event handler that is executed when this element receives a mouse up event.
	 */
	protected String onmouseup;

	//  Navigation Management

	/**
	 * Access key character.
	 */
	protected String accesskey;

	/**
	 * Tab index value.
	 */
	protected String tabindex;

    /**
     * Gets whether or not this element is disabled
     *
     * @return <code>Boolean.TRUE</code> if this element is disabled
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.Boolean"
	 * description="Whether or not this element is disabled"
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * Sets whether or not this element is disabled
     *
     * @param disabled whether this element is disabled
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

	/**
     * Returns CSS style assigned to this element
     *
	 * @return CSS style
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="CSS styles to be applied to this HTML element"
	 */
	public String getStyle() {
		return style;
	}

    /**
     * Sets CSS style assigned to this element
     *
     * @param style CSS style to set
     */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
     * Returns style class assigned to this element
     *
	 * @return style class
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="CSS stylesheet class to be applied to this HTML element (renders a "class" attribute). "
	 */
	public String getStyleClass() {
		return styleClass;
	}

    /**
     * Sets style class assigned to this element
     *
     * @param styleClass style class to set
     */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

    /**
     * Gets ID for the generated tag
     *
     * @return ID of tag
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="The ID for the generated tag"
     */
    public String getTagId() {
        return tagId;
    }

    /**
     * Sets ID for the generated tag
     *
     * @param tagId ID of tag
     */
    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

	/**
     * Returns alt value
     *
	 * @return alt value
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="The alternate text of this element"
	 */
	public String getAlt() {
		return alt;
	}

    /**
     * Sets alt value
     *
     * @param alt alt value to set
     */
	public void setAlt(String alt) {
		this.alt = alt;
	}

	/**
     * Returns title
     *
	 * @return title
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="The advisory title of this element"
	 */
	public String getTitle() {
		return title;
	}

    /**
     * Sets title
     *
     * @param title title to set
     */
	public void setTitle(String title) {
		this.title = title;
	}

	// JavaScript events getters/setters

    /**
     * Gets JS code that is executed when this element's content is being changed
     *
     * @return the JS onchange handler
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element's content changes."
     */
    public String getOnchange() {
        return onchange;
    }

    /**
     * Sets JS code that is executed when this element's content is being changed
     *
     * @param onchange the JS onchange handler
     */
    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }

	/**
     * Returns JS code that is executed when this element loses focus
     *
	 * @return the JS onblur handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element loses input focus."
	 */
	public String getOnblur() {
		return onblur;
	}

    /**
     * Sets JS code that is executed when this element loses focus
     *
     * @param onblur the JS onblur handler
     */
	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	/**
     * Returns JS code that is executed when this element is clicked
     *
	 * @return the JS onclick handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse click."
	 */
	public String getOnclick() {
		return onclick;
	}

    /**
     * Sets JS code that is executed when this element is clicked
     *
     * @param onclick the JS onclick handler
     */
	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	/**
     * Returns JS code that is executed when this element is double-clicked
     *
	 * @return the JS ondblclick handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives mouse double click"
	 */
	public String getOndblclick() {
		return ondblclick;
	}

    /**
     * Sets JS code that is executed when this element is double-clicked
     *
     * @param ondblclick the JS ondblclick handler
     */
	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}

	/**
     * Returns JS code that is executed when this element gets focus
     *
	 * @return the JS onfocus handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives input focus"
	 */
	public String getOnfocus() {
		return onfocus;
	}

    /**
     * Sets JS code that is executed when this element gets focus
     *
     * @param onfocus the JS onfocus handler
     */
	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	/**
     * Returns JS code that is executed when a key is pressed
     *
	 * @return the JS onkeydown handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a key down event"
	 */
	public String getOnkeydown() {
		return onkeydown;
	}

    /**
     * Sets JS code that is executed when a key is pressed
     *
     * @param onkeydown the JS onkeydown handler
     */
	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}

	/**
     * Returns JS code that is executed when a key is pressed and released
     *
	 * @return the JS onkeypress handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a key press event"
	 */
	public String getOnkeypress() {
		return onkeypress;
	}

    /**
     * Sets JS code that is executed when a key is pressed and released
     *
     * @param onkeypress the JS onkeypress handler
     */
	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}

	/**
     * Returns JS code that is executed when a key is released
     *
	 * @return the JS onkeyup handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a key up event"
	 */
	public String getOnkeyup() {
		return onkeyup;
	}

    /**
     * Gets JS code that is executed when a key is released
     *
     * @param onkeyup the JS onkeyup handler
     */
	public void setOnkeyup(String onkeyup) {
		this.onkeyup = onkeyup;
	}

	/**
     * Returns JS code that is executed when a mouse button is pressed
     *
	 * @return the JS onmousedown handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse down event"
	 */
	public String getOnmousedown() {
		return onmousedown;
	}

    /**
     * Gets JS code that is executed when a mouse button is pressed
     *
     * @param onmousedown the JS onmousedown handler
     */
	public void setOnmousedown(String onmousedown) {
		this.onmousedown = onmousedown;
	}

	/**
     * Returns JS code that is executed when a mouse is moved
     *
	 * @return the JS onmousemove handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse move event"
	 */
	public String getOnmousemove() {
		return onmousemove;
	}

    /**
     * Sets JS code that is executed when a mouse is moved
     *
     * @param onmousemove the JS onmousemove handler
     */
	public void setOnmousemove(String onmousemove) {
		this.onmousemove = onmousemove;
	}

	/**
     * Returns JS code that is executed when a mouse leaves this element
     *
	 * @return the JS onmouseout handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse out event"
	 */
	public String getOnmouseout() {
		return onmouseout;
	}

    /**
     * Sets JS code that is executed when a mouse leaves this element
     *
     * @param onmouseout the JS onmouseout handler
     */
	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}

	/**
     * Returns JS code that is executed when a mouse enters this element
     *
	 * @return the JS onmouseover handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse over event"
	 */
	public String getOnmouseover() {
		return onmouseover;
	}

    /**
     * Sets JS code that is executed when a mouse leaves this element
     *
     * @param onmouseover the JS onmouseover handler
     */
	public void setOnmouseover(String onmouseover) {
		this.onmouseover = onmouseover;
	}

	/**
     * Returns JS code that is executed when a mouse button is released
     *
	 * @return the JS onmouseup handler
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="JavaScript event handler that is executed when this element receives a mouse up event"
	 */
	public String getOnmouseup() {
		return onmouseup;
	}

    /**
     * Sets JS code that is executed when a mouse button is released
     *
     * @param onmouseup the JS onmouseup handler
     */
	public void setOnmouseup(String onmouseup) {
		this.onmouseup = onmouseup;
	}

	/**
     * Returns access key that's used to quickly select this element
     *
	 * @return access key
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Keyboard character used to immediately select this element"
	 */
	public String getAccesskey() {
		return accesskey;
	}

    /**
     * Sets access key that's used to quickly select this element
     *
     * @param accesskey access key
     */
	public void setAccesskey(String accesskey) {
		this.accesskey = accesskey;
	}

	/**
     * Returns tab index of this element
     *
	 * @return tab index
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Tab index"
	 */
	public String getTabindex() {
		return tabindex;
	}

    /**
     * Sets tab index of this element
     *
     * @param tabindex tab index to set
     */
	public void setTabindex(String tabindex) {
		this.tabindex = tabindex;
	}

	/**
	 * Concatenates all attributes which were set into string buffer
	 *
	 * @return all set attributes with their values
	 */
	protected StringBuffer prepareAttributes() {
		StringBuffer result = new StringBuffer();

        if (disabled == null) {
            disabled = Boolean.FALSE;
        }
        if ( disabled.booleanValue() ) {
			result.append(" disabled=\"true\"");
		}

		if ( style != null && style.length() != 0 ) {
			result.append(" style=\"" + style + "\"");
		}

		if ( styleClass != null && styleClass.length() != 0 ) {
			result.append(" class=\"" + styleClass + "\"");
		}

		if ( tagId != null && tagId.length() != 0 ) {
			result.append(" id=\"" + tagId + "\"");
		}

		if ( alt != null && alt.length() != 0 ) {
			result.append(" alt=\"" + alt + "\"");
		}

		if ( title != null && title.length() != 0 ) {
			result.append(" title=\"" + title + "\"");
		}

        if ( onchange != null && onchange.length() != 0 ) {
			result.append(" onchange=\"" + onchange + "\"");
		}

		if ( onblur != null && onblur.length() != 0 ) {
			result.append(" onblur=\"" + onblur + "\"");
		}

		if ( onclick != null && onclick.length() != 0 ) {
			result.append(" onclick=\"" + onclick + "\"");
		}

		if ( ondblclick != null && ondblclick.length() != 0 ) {
			result.append(" ondblclick=\"" + ondblclick + "\"");
		}

		if ( onfocus != null && onfocus.length() != 0 ) {
			result.append(" onfocus=\"" + onfocus + "\"");
		}

		if ( onkeydown != null && onkeydown.length() != 0 ) {
			result.append(" onkeydown=\"" + onkeydown + "\"");
		}

		if ( onkeypress != null && onkeypress.length() != 0 ) {
			result.append(" onkeypress=\"" + onkeypress + "\"");
		}

		if ( onkeyup != null && onkeyup.length() != 0 ) {
			result.append(" onkeyup=\"" + onkeyup + "\"");
		}

		if ( onmousedown != null && onmousedown.length() != 0 ) {
			result.append(" onmousedown=\"" + onmousedown + "\"");
		}

		if ( onmousemove != null && onmousemove.length() != 0 ) {
			result.append(" onmousemove=\"" + onmousemove + "\"");
		}

		if ( onmouseout != null && onmouseout.length() != 0 ) {
			result.append(" onmouseout=\"" + onmouseout + "\"");
		}

		if ( onmouseover != null && onmouseover.length() != 0 ) {
			result.append(" onmouseover=\"" + onmouseover + "\"");
		}

		if ( onmouseup != null && onmouseup.length() != 0 ) {
			result.append(" onmouseup=\"" + onmouseup + "\"");
		}

		if ( accesskey != null && accesskey.length() != 0 ) {
			result.append(" accesskey=\"" + accesskey + "\"");
		}

		if ( tabindex != null && tabindex.length() != 0 ) {
			result.append(" tabindex=\"" + tabindex + "\"");
		}

		return result;
	}

}
